//
//  BaseTabbarVC.swift
//  WebViewGoback
//
//  Created by 郑东喜 on 2017/2/24.
//  Copyright © 2017年 郑东喜. All rights reserved.
//  修改tabbar的文字和图片位置

import UIKit

class BaseTabbarVC: UITabBarController {
    
    static let shared = BaseTabbarVC()
    
    //tabbar的高度
    lazy var myHeight: CGFloat = {
        let hei : CGFloat = self.tabBar.frame.height

        return hei
    }()

    //tabbar titleLabel 坐标 内间距 上边的值（默认0，为了好看 给他2）
    var tabUppos : Int = 2
    
    //tabbar titleLabel 坐标 内间距 下边的值（默认0，为了好看 给他-2）
    var tabDownpos : Int = -2
    
    //中间是否显示大的按钮
    var centerIsBig : Bool?
    
    //tabar样式配置字典
    lazy var dataSource: NSDictionary = {
        
        var ad : NSDictionary = NSDictionary()
        
        return ad
    }()

    //存放子视图
    fileprivate var viewArray = [UIView]()

    override func viewDidLoad() {
        super.viewDidLoad()
    
        //本地tabbar配置
        self.tabbarConfig()
        
    }

    //在这里修改
    override func viewWillLayoutSubviews() {
        ///调整tabbar文本的高度
        UITabBarItem.appearance().titlePositionAdjustment = UIOffsetMake(0.0, -5.0)
        
        //重新设置tabbar 的高度
        var frame = self.tabBar.frame
        frame.size.height = self.myHeight
        frame.origin.y = self.view.frame.size.height - self.myHeight
        self.tabBar.frame = frame
        self.tabBar.barStyle = .default
        
        //设置图片偏移，因为加高了tabbar-----只是修改tabbar的图片位置
        if self.tabBar.frame.width != 0 {
            for i in 0..<self.tabBar.items!.count {
                let itm = self.tabBar.items![i]
                
                if i != 2 {
                    itm.imageInsets = UIEdgeInsets(top: self.myHeight - 49 - CGFloat(self.tabUppos), left: 0, bottom: -(CGFloat(self.myHeight) - 49 - CGFloat(self.tabUppos)), right: 0)
                    
                    itm.imageInsets = UIEdgeInsets(top: CGFloat(self.tabUppos), left: 0, bottom: CGFloat(self.tabDownpos), right: 0)
                    
                } else {
                    itm.imageInsets = UIEdgeInsets(top: CGFloat(self.tabUppos), left: 0, bottom: CGFloat(self.tabDownpos), right: 0)
                }
            }
        }
    }
    
    override func viewDidLayoutSubviews() {
        
        self.viewArray.removeAll()
        
        for index in 0..<self.tabBar.subviews.count {
            
            let view = self.tabBar.subviews[index]
            self.viewArray.append(view)
        }
        
        for i in 0..<self.viewArray.count {
            
            let v = self.viewArray[i]
            v.layer.removeAllAnimations()
        }
        
        super.viewDidLayoutSubviews()
        
        self.tabBar.barTintColor = UIColor.green
    }
}

extension BaseTabbarVC {
    //本地tabbar配置文件参数
    fileprivate func fileIsExist() -> Bool {
        let fileManager = FileManager.default
        let filePath:String = NSHomeDirectory() + "/Documents/bj/tabbar.plist"
        let exist = fileManager.fileExists(atPath: filePath)

        return exist
    }
}

extension BaseTabbarVC {
    
    /// tabbar配置
    fileprivate func tabbarConfig() -> Void {
        //判断本地的tabbar配置文件是否存在
        if self.fileIsExist() == true {
            //存在即从本地读取配置文件、取出对应的配置参数进行tabar的控制。
            self.dataSource = NSDictionary(contentsOfFile:  NSHomeDirectory() + "/Documents/bj/tabbar.plist")!
            
            if self.dataSource.count == 0 {
                
            } else {
                
                //赋值上面的值
                self.tabUppos = (self.dataSource["barTitleTop"] as? Int)!
                
                //赋值底部的值
                self.tabDownpos = (self.dataSource["barTitleBottom"] as? Int)!
                
                //中间是否显示大的按钮
                self.centerIsBig = self.dataSource["centerShowWIthRound"] as? Bool
                
                if self.centerIsBig == true {
                    
                    //清楚顶部的线条
                    self.clearTabBarTopLine()
                    
                    self.myHeight = 64
                    
                    //绘制大的按钮
                    self.drawBigBtn()
                    
                    self.tabBar.barTintColor = UIColor.red
                }
            }
            
        } else {
            //请求网络接口，写入本地
        }
    }
}

// MARK:- 绘制大按钮
extension BaseTabbarVC {
    fileprivate func drawBigBtn() -> Void {
        //添加顶部的线条以及圆弧
        let bgV = LineVeiw(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: myHeight))
        bgV.backgroundColor = UIColor.groupTableViewBackground
        self.tabBar.addSubview(bgV)
    }
    
    //MARK:清除顶部线条
    fileprivate func clearTabBarTopLine(){
        
        let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        UIGraphicsBeginImageContext(rect.size)
        
        let context = UIGraphicsGetCurrentContext()
        
        context?.setFillColor(UIColor.clear.cgColor)
        
        context?.fill(rect)
        
        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.tabBar.backgroundImage = img
        self.tabBar.shadowImage = img
    }

}
